home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / cutils.arc / FILE.ASM < prev    next >
Assembly Source File  |  1985-08-30  |  7KB  |  186 lines

  1.                         TITLE   File Manipulation Routines for Lattice
  2.                         NAME    FILES
  3.                         INCLUDE DOS.MAC
  4.  
  5. COMMENT #
  6.                 AUTHOR          Jon Wesener
  7.                 DATE            July 9, 1985
  8.                 PURPOSE           To supply fast routines for C
  9.                                 and Assembler programs.  This section
  10.                                 is for file manipulation and needs the
  11.                                 include file FILES.H .
  12.                 (C) Copyright 1985- Jon Wesener.
  13.         #
  14.  
  15.                 DSEG
  16. BUFSIZE         EQU     128
  17. buffer          db      BUFSIZE dup (?)
  18. bcnt            dw      0
  19. bufptr          dw      ?
  20.                 ENDDS
  21.  
  22.                 PSEG
  23.                 PUBLIC  FCREATE, FOPEN, FCLOSE, FREAD, FWRITE, FMOVE, FDEL
  24.                 PUBLIC  FREN, GETCH
  25. ; Create a newfile or truncate an existing file.
  26. ;  fcreate(name,attr)
  27. ; RETURN: -1= error     ?= channel
  28.  
  29. FCREATE         PROC    NEAR
  30.                 push    bp
  31.                 mov     bp, sp
  32.                 mov     ax, 3c00h       ; dos function for create
  33.                 mov     cx, [bp+6]      ; get the file attribute
  34.                 mov     dx, [bp+4]      ; get the file name
  35.                 int     21h
  36.                 jnc     fcrex           ; no problem
  37.                 mov     ax, -1          ; signal error
  38. fcrex:          pop     bp
  39.                 ret
  40. FCREATE         ENDP
  41.  
  42. ; open an existing file for read, write, or update io.
  43. ;  fopen(name,access)
  44. ; RETURN: -1= error    ?= channel
  45.  
  46. FOPEN           PROC    NEAR
  47.                 push    bp
  48.                 mov     bp, sp
  49.                 mov     ax, [bp+6]      ; get file access
  50.                 mov     ah, 3dh         ; dos open file function
  51.                 mov     dx, [bp+4]      ; get file name
  52.                 int     21h
  53.                 jnc     fopex
  54.                 mov     ax, -1          ; signal error
  55. fopex:          pop     bp
  56.                 ret
  57. FOPEN           ENDP
  58.  
  59. ; fclose closes a channel opened with fcreate or fopen.
  60. ;  fclose(channel)
  61. ; RETURN: 0= successful         ?= error
  62.  
  63. FCLOSE          PROC    NEAR
  64.                 push    bp
  65.                 mov     bp, sp
  66.                 mov     ax, 3e00h       ; dos close file function
  67.                 mov     bx, [bp+4]      ; get channel
  68.                 int     21h
  69.                 jc      fclex
  70.                 xor     ax, ax          ; signal success
  71. fclex:          pop     bp
  72.                 ret
  73. FCLOSE          ENDP
  74.  
  75. ; fread reads a number of bytes from an open channel
  76. ;  fread(channel, buffer, count)
  77. ; RETURN: Number of bytes read.
  78.  
  79. FREAD           PROC    NEAR
  80.                 push    bp
  81.                 mov     bp, sp
  82.                 mov     ax, 3f00h       ; dos read function
  83.                 mov     bx, [bp+4]      ; get channel
  84.                 mov     cx, [bp+8]      ; get count
  85.                 mov     dx, [bp+6]      ; get buffer
  86.                 int     21h
  87.                 pop     bp
  88.                 ret
  89. FREAD           ENDP
  90.  
  91. ; fwrite writes a number of bytes to an open channel
  92. ;  fwrite(channel, buffer, count)
  93. ; RETURN: Number of bytes written
  94.  
  95. FWRITE          PROC    NEAR
  96.                 push    bp
  97.                 mov     bp, sp
  98.                 mov     ax, 4000h       ; dos read function
  99.                 mov     bx, [bp+4]      ; get channel
  100.                 mov     cx, [bp+8]      ; get count
  101.                 mov     dx, [bp+6]      ; get buffer
  102.                 int     21h
  103.                 pop     bp
  104.                 ret
  105. FWRITE          ENDP
  106.  
  107. ; fmove moves the file pointer a number of bytes relative to base
  108. ;  fmove(channel, long count, base)
  109. ; RETURN: 0= success    ?= error
  110.  
  111. FMOVE           PROC    NEAR
  112.                 push    bp
  113.                 mov     bp, sp
  114.                 mov     ax, [bp+10]     ; get base
  115.                 mov     ah, 42h         ; dos move file pointer function
  116.                 mov     bx, [bp+4]      ; get channel
  117.                 mov     cx, [bp+6]      ; get low count
  118.                 mov     dx, [bp+8]      ; get high count
  119. FMOVE           ENDP
  120.  
  121. ; fdel deletes the named file
  122. ;  fdel(name)
  123. ; RETURN: 0= success    ?= error
  124.  
  125. FDEL            PROC    NEAR
  126.                 push    bp
  127.                 mov     bp, sp
  128.                 mov     ax, 4100h       ; dos delete file function
  129.                 mov     dx, [bp+4]      ; get file name
  130.                 int     21h
  131.                 jc      fdex
  132.                 xor     ax, ax          ; signal success
  133. fdex:           pop     bp
  134.                 ret
  135. FDEL            ENDP
  136.  
  137. ; Fren will rename a file.
  138. ;  err= fren(to, from);
  139. ; RETURN:       0= Success      ?= Error
  140.  
  141. FREN            PROC    NEAR
  142.                 push    bp
  143.                 mov     bp, sp
  144.                 mov     ax, 5600h
  145.                 mov     di, [bp+4]      ; get new name
  146.                 mov     dx, [bp+6]      ; get old name
  147.                 int     21h             ; change it
  148.                 jc      frenex
  149.                 xor     ax, ax          ; signal success
  150. frenex:         pop     bp
  151.                 ret
  152. FREN            ENDP
  153.  
  154. ; getch gives buffered single character io for files
  155. ;  getch(&ch , channel);
  156. ; RETURN: 0= EOF        1= character ready
  157. ; NOTE:         Use this for 1 file only, and consistently.
  158.  
  159. GETCH           PROC    NEAR
  160.                 push    bp
  161.                 mov     bp, sp
  162.                 test    bcnt, 0ffffh    ; anything in the buffer
  163.                 jnz     getc1           ; yes
  164.                 mov     dx, offset buffer
  165.                 mov     bufptr, dx      ; save ptr into buffer
  166.                 mov     cx, BUFSIZE
  167.                 mov     bx, [bp+6]      ; channel to read
  168.                 mov     ax, 3f00h
  169.                 int     21h             ; read it in
  170.                 and     ax, ax          ; is it eof?
  171.                 jz      getc2
  172.                 mov     bcnt, ax        ; store buffer cnt
  173. getc1:          mov     si, bufptr      ; get ptr into buffer
  174.                 mov     di, [bp+4]      ; get ptr to character
  175.                 movsb
  176.                 inc     bufptr          ; pt to next character
  177.                 dec     bcnt            ; set count
  178.                 mov     ax, 1           ; signal success
  179. getex:          pop     bp
  180.                 ret
  181. getc2:          xor     ax, ax          ; signal error
  182.                 jmp     getex
  183. GETCH           ENDP
  184.                 ENDPS
  185.                 END
  186.